
import http from '@ohos.net.http';
import NewsInfo from '../pages/viewmodel/NewsInfo';

class NewsModel{

  baseUrl:string = 'http://api.webxd.top'
  pageNumber:number = 0
  reqToken:string = 'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDU1MDA2ODMsInVzZXJfbmFtZSI6ImZmZGQiLCJ1c2VyX2lkIjoxMH0.ZGGiosAG411D0QjjaIe52xtvkAxkmUyOkr772j7bJ3jd_7zpJnhLFBaB792Chal6DW2nLtDLXVizVtc0VbZ_zA'
  getNewsList(): Promise<NewsInfo[]> {

    return new Promise((resolve,reject)=>{
      //  1、创建http请求
      let httpRequest = http.createHttp()
      console.log('发送请求')
      // 2、发送请求
      httpRequest.request(
        `${this.baseUrl}/news_info?page=${this.pageNumber}&rows=5`,
        {
          method:http.RequestMethod.GET,
          header:{
            'Authorization':this.reqToken
          }
        }
      ).then(resp=>{
        if (resp.responseCode === 200){
          // 告知查询成功
          console.log('查询成功')


          resolve(JSON.parse(resp.result.toString()).data)
        }else{
          console.log('查询失败')
          console.log('请求出错，error:',JSON.stringify(resp))
          reject('查询失败')
        }

      }).catch(
        error=>{
          console.log('查询失败')
          console.log('请求出错，error:',JSON.stringify(error))
          reject('查询失败')
        }
      )
    }
    )




  }
}


const newsModel = new  NewsModel();
export default  newsModel as NewsModel;